layui.use(['table', 'admin', 'ax', 'ztree', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var form = layui.form;

    /**
     * 通用编码管理
     */
    var BasicType = {
        tableId: "basicTypeTable",
        condition: {
            id: ""
        }
    };

    var gsExecuted2 = false;
    BasicType.onClickBasicCode = function (e, treeId, treeNode) {
        BasicType.condition.id = treeNode.id
        if (!gsExecuted) {
            if (!gsExecuted2) {
                gs();
                gsExecuted2 = true;
                form.on('select(xmFilter)', function (data) {
                    var lb = data.value //得到被选中的值
                    $("#gsName").val(lb);
                });
            }
        }
        $("#legend").text("修改编码");
        //获取详情信息，填充表单
        var ajax = new $ax(Feng.ctxPath + "/basicType/detail?id=" + treeNode.id);
        var result = ajax.start();
        form.val('basicTypeForm', result.data);
        $("#butshow").show();
        submitType = 2

    }
    function gs() {
        $.ajax({
            url: Feng.ctxPath + "/basicTypeGs/list2",
            dataType: 'json',
            type: 'get',
            async: false,
            success: function (data) {
                console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select = document.getElementById('gsName');
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.gsName; // 设置选项的值
                    option.text = item.gsName;
                    select.add(option);
                });
                layui.form.render("select");
            }
        });
    }
    /**
     * 点击添加
     */
    var gsExecuted = false;
    $('#btnAdd').click(function () {
        if (!gsExecuted2) {
            if (!gsExecuted) {
                gs();
                gsExecuted = true;
            }
        }
        $("#legend").text("添加编码");
        $("#basicTypeForm")[0].reset();
        layui.form.render();
        if(BasicType.condition.id == ""){
            $("#pid").val("0");
        } else {
            $("#pid").val(BasicType.condition.id);
        }
        $("#butshow").show();
        submitType = 1

    });

    /**
     * 弹出添加对话框
     *///表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        if(submitType == 1){
            var ajax = new $ax(Feng.ctxPath + "/basicType/addItem", function (data) {
                Feng.success("添加成功！");
                $("#basicTypeForm")[0].reset();
                layui.form.render();
                if(BasicType.condition.id == ""){
                    $("#pid").val("0");
                } else {
                    $("#pid").val(BasicType.condition.id);
                }
                ztree.init();//重新加载树
            }, function (data) {
                Feng.error("添加失败！" + data.responseJSON.message)
            });
            ajax.set(data.field);
            ajax.start();
        }
        if(submitType == 2){
            var ajax = new $ax(Feng.ctxPath + "/basicType/editItem", function (data) {
                Feng.success("更新成功！");
                ztree.init();//重新加载树
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data.field);
            ajax.start();
        }

        return false;
    });


    /**
     * 点击删除
     */
    $('#delete').click(function () {
        if(submitType == 1){
            $("#basicTypeForm")[0].reset();
            layui.form.render();
            if(BasicType.condition.id == ""){
                $("#pid").val("0");
            } else {
                $("#pid").val(BasicType.condition.id);
            }
        }
        if(submitType == 2){
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/basicType/delete", function (data) {
                    Feng.success("删除成功!");
                    $("#butshow").hide();
                    $("#basicTypeForm")[0].reset();
                    layui.form.render();
                    ztree.init();//重新加载树
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", BasicType.condition.id);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        }
    });

    //初始化左侧部门树
    var ztree = new $ZTree("typeTree", "/basicType/tree");
    ztree.bindOnClick(BasicType.onClickBasicCode);
    ztree.init();
});
